﻿<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>API</title>
    <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../bootstrap/css/pub.css" rel="stylesheet" />
    <script src="../bootstrap/js/jquery.min.js"></script>
    <script src="../bootstrap/js/bootstrap.min.js"></script>
    <script src="../bootstrap/js/bootstrap-paginator.js"></script>
    <!--[if lt IE 9]>
      <script src="bootstrap/js/html5shiv.min.js"></script>
      <script src="bootstrap/js/respond.min.js"></script>
    <![endif]-->
    <script src="../config.js"></script>
    <script src="../bootstrap/js/Common.js"></script>
    <script src="../bootstrap/js/controls.js"></script>
    <script type="text/javascript">
        $(function () {
            init(1);
        })
        function init(pgindex)
        {
            //菜单
            var menus = [
                { name: "新增", action: "openAddWin()", css: "btn-primary" },
                { name: "编辑", action: "openEditWin()", css: "btn-primary" },
                { name: "删除", action: "openDelWin()", css: "btn-primary" }
            ];
            $(".menu-container").menu({ items: menus });
            //列表
            var columns = [
                { name: "编号", field: "terminalID" }, { name: "型号", field: "model" }, { name: "名称", field: "name" },
                { name: "本机地址", field: "localAddress" }, { name: "访问地址", field: "remoteAddress" }, { name: "访问密码", field: "accessKey" }, { name: "协议", field: "protocol" },
                { name: "软件版本", field: "version" }, { name: "位置坐标", field: "coordinate" }, { name: "地址", field: "address" },
                { name: "心跳超时秒数", field: "timeoutSecs" },
                //{ name: "扩展信息", field: "extras" },
                {
                    name: "操作",
                    field: "terminalID",
                    title: '操作',
                    formatter: function (value) {
                        return '<a href="javascript:void(0);" onclick="Reboot(\'' +
                            value +
                            '\')" class="btn btn-sm btn-info" ><i class="fa fa-trash-o"></i>重启</a>' +
                            '&nbsp&nbsp&nbsp<a href="javascript:void(0);" onclick="Reset(\'' +
                            value +
                            '\')" class="btn btn-sm btn-info" ><i class="fa fa-trash-o"></i>重置</a>' +
                            '&nbsp&nbsp&nbsp<a href="javascript:void(0);" onclick="Upload(\'' +
                            value +
                            '\')"  class="btn btn-sm btn-info" ><i class="fa fa-trash-o"></i>配置上传</a>';
                    }
                }
            ];
            //分页
            var param = { pagesize: pagesize, pageindex: pgindex };
            $("#tab").datagrid({ columns: columns, dblclick: openEditWin }).loadData(host_name + "terminal/list", "post", param, function () {
                $.ajax({
                    url: host_name + "terminal/count",
                    type: "get",
                    success: function (data) {
                        if (data < 10) {
                            $("#example").hide();
                        }
                        else {
                            $("#example").show();
                            totalcount = Math.ceil(data / pagesize)
                            $('#pageLimit').bootstrapPaginator({
                                currentPage: pgindex,
                                totalPages: totalcount,//总页数
                                numberOfPages: pagenum,//显示页数
                                size: "normal",
                                bootstrapMajorVersion: 3,
                                alignment: "right",
                                itemTexts: function (type, page, current) {
                                    switch (type) {
                                        case "first": return "首页";
                                        case "prev": return "上一页";
                                        case "next": return "下一页";
                                        case "last": return "末页";
                                        case "page": return page;
                                    }//默认显示的是第一页。
                                },
                                onPageClicked: function (event, originalEvent, type, page) {//给每个页眉绑定一个事件，其实就是ajax请求，其中page变量为当前点击的页上的数字。
                                    init(page);
                                }
                            });
                        }
                    }
                });
            }); 
        }
        //重启
        function Reboot(val) {
            $.ajax({
                url: cmd_url + "IOVar/" + val,
                type: "post",
                data: { "action": 3 },
                success: function (data) {
                    if (data && data.code == 200) {
                        alert("重启成功！");
                    }
                    else {
                        alert("重启失败！");
                    }
                },
                error: function(request) {
                alert("连接失败");
            }
            });
        }
        //重置
        function Reset(val) {
            $.ajax({
                url: cmd_url +"terminalcommand/"+ val,
                type: "post",
                data: { "action": 2 },
                success: function (data) {
                    if (data && data.code == 200) {
                        alert("重置成功！");
                    }
                    else {
                        alert("重置失败！");
                    }
                },
                error: function (request) {
                    alert("连接失败");
                }
            });
        }
        //上传配置
        function Upload(val) {
            $.ajax({
                url: host_name +"UpLoadConfigFile/"+ val,
                type: "get",
                success: function (data) {
                    if (data && data.code == 200) {
                        alert("上传成功！");
                    }
                    else {
                        alert("上传失败！");
                    }
                },
                error: function (request) {
                    alert("连接失败");
                }
            });
        }
        //新增
        function openAddWin() {
            openModalWin("新增终端", "terminaledit.html?mode=1", refresh, 700, 650);

        }
        //编辑
        function openEditWin() {
            var row = $("#tab").datagrid().getSelected();
            if (row.length > 0) {
                openModalWin("编辑终端", "terminaledit.html?mode=2&id=" + row[0].id, refresh, 700, 650);
            } else {
                alert("请选择要编辑的数据！");
            }
        }
        //删除
        function openDelWin() {
            var row = $("#tab").datagrid().getSelected();
            if (row.length > 0) {
                var delUrl = host_name + "terminal/del?id=" + row[0].id;
                if (confirm("确定要删除选定的数据吗？")) {
                    $.ajax({
                        url: delUrl,
                        type: "get",
                        success: function (data) {
                            refresh();
                        }
                    });
                }
            }
            else {
                alert("请选择要删除的数据！");
            }
        }
        //刷新
        function refresh() {
            $("#tab").datagrid().refresh();
        }
    </script>
</head>
<body>
    <div class="container-fluid">
        <div class="row" style="margin-bottom:10px">
            <div class="col-md-2">
                <h4>终端列表</h4>
            </div>
            <div class="col-md-10">
                <div class="menu-container pull-right">

                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12 table-responsive">
                <table id="tab" class="table table-bordered table-hover"></table>
            </div>
        </div>
        <div class="row" style="margin-right: 10px;">
            <div id="example" style="text-align: right">
                <ul id="pageLimit"></ul>
            </div>
        </div>
    </div>
</body>
</html>

